﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Calculator
{
    public partial class TrigFunctionControl : UserControl
    {
        private Button _selectedButton; // 当前选中的按钮
        private string _selectedFunction; // 当前选中的三角函数

        public TrigFunctionControl()
        {
            InitializeComponent();
        }

        // 选中三角函数按钮
        private void FunctionButton_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            // 重置之前选中的按钮颜色
            if (_selectedButton != null)
            {
                _selectedButton.Background = Brushes.LightGray;
            }

            // 设置当前选中按钮
            _selectedButton = button;
            _selectedButton.Background = Brushes.LightGreen;

            // 记录选中的三角函数
            _selectedFunction = button.Content.ToString();
            ResultDisplay.Text = $"{_selectedFunction} 准备计算，请输入数值。";
        }

        // 点击等号按钮进行计算
        private void EqualsButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_selectedFunction))
            {
                ResultDisplay.Text = "请先选择三角函数！";
                return;
            }

            if (!double.TryParse(InputBox.Text, out double input))
            {
                ResultDisplay.Text = "请输入有效的数值！";
                return;
            }

            double result = 0;
            switch (_selectedFunction)
            {
                case "sin":
                    result = Math.Sin(input * Math.PI / 180);
                    break;
                case "cos":
                    result = Math.Cos(input * Math.PI / 180);
                    break;
                case "tan":
                    result = Math.Tan(input * Math.PI / 180);
                    break;
                case "arcsin":
                    if (input < -1 || input > 1)
                    {
                        ResultDisplay.Text = "arcsin 输入超出范围（-1到1）！";
                        return;
                    }
                    result = Math.Asin(input) * (180 / Math.PI);
                    break;
                case "arccos":
                    if (input < -1 || input > 1)
                    {
                        ResultDisplay.Text = "arccos 输入超出范围（-1到1）！";
                        return;
                    }
                    result = Math.Acos(input) * (180 / Math.PI);
                    break;
                case "arctan":
                    result = Math.Atan(input) * (180 / Math.PI);
                    break;
                default:
                    ResultDisplay.Text = "未知三角函数！";
                    return;
            }

            // 显示计算结果
            ResultDisplay.Text = $"{_selectedFunction}({input}) = {result}";
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox.Text == textBox.Tag.ToString())
            {
                textBox.Text = ""; // 清空占位文本
                textBox.Foreground = Brushes.Black; // 设置正常文本颜色
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                textBox.Text = textBox.Tag.ToString(); // 恢复占位文本
                textBox.Foreground = Brushes.Gray; // 设置占位文本颜色
            }
        }
    }
}
